//这里主要是获取用户的state(状态)
function getState() {
  var username = document.getElementById("username").value;
  // 1.获取异步交互对象
  var xmlhttp = createXMLHttpRequest();
  // 2.设置监听
  xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      if (xmlhttp.responseText == 1) {
        document.getElementById("state").value = xmlhttp.responseText;
        //return;
      }
      //document.getElementById("u").innerHTML = xmlhttp.responseText;
    }
  };
  // 3.打开连接(为了避免浏览器的缓存，需要加一个时间参数)
  xmlhttp.open("GET", "/shop/user_getState?a=" + new Date().getTime()
      + "&username=" + username + "", true);
  // 4.发送请求
  xmlhttp.send(null);
}

// 创建XMLHttpRequest对象
function createXMLHttpRequest() {
  try {
    return new XMLHttpRequest();
  } catch (e) {
    try {
      return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        return new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("你用什么浏览器呢？");
        throw e;
      }
    }
  }
};

//登录表单的非空校验
function check() {
  var username = document.getElementById("username").value;
  if (username == null || username == "") {
    alert("用户名不能为空");
    return false;
  }
  var password = document.getElementById("password").value;
  if (password == null || password == "") {
    alert("密码不能为空");
    return false;
  }
  var validatecode = document.getElementById("validatecode").value;
  if (validatecode == null || validatecode == "") {
    alert("验证码不能为空");
    return false;
  }
}

//切换验证码
function changecode(obj) {
  obj.src = "/getCode.do?a=" + new Date().getTime();
}


function startLogin() {
  var loading = layer.load(0);
  var user = {};
  var loginResult = "";
  user.userNameOrEmail = document.getElementById("inputEmail").value;
  user.password = document.getElementById("inputPassword").value;
  $.ajax({
    async: false,
    type: 'POST',
    url: '${pageContext.request.contextPath}/doLogin',
    data: user,
    dataType: 'json',
    success: function (result) {
      loginResult = result.result;
      layer.close(loading);
    },
    error: function (result) {
      layer.alert('查询用户错误');
    }
  });

  if (loginResult == 'success') {
    layer.msg('登录成功', {icon: 1});
    window.location.href = "${cp}/main";
  }
  else if (loginResult == 'unexist') {
    layer.msg('是不是用户名记错了？', {icon: 2});
  }
  else if (loginResult == 'wrong') {
    layer.msg('密码不对哦，再想想~', {icon: 2});
  }
  else if (loginResult == 'fail') {
    layer.msg('服务器异常', {icon: 2});
  }

}



